package com.travel.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.xy.XYDataset;

import com.travel.core.DaoFactory;
import com.travel.dao.ISpotDao;
import com.travel.service.DrawBar;
import com.travel.service.DrawLine;
import com.travel.service.DrawPie;

public class JfreeGuilinTour extends HttpServlet {

    private static final long serialVersionUID = 8891755440833155297L;

    private static final Logger LOGGER = LogManager.getLogger(JfreeGuilinTour.class);

    private static final ISpotDao SPOT_DAO = DaoFactory.getDao(ISpotDao.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("contentType", "text/html; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        OutputStream out = response.getOutputStream();

        String currLoc = request.getParameter("loc");
        if (StringUtils.isBlank(currLoc)) {
            currLoc = "0773";// 默认显示桂林
        }
        String city = null;
        if (StringUtils.equals("0773", currLoc)) {
            city = "桂林市";
        }
        if (StringUtils.equals("0771", currLoc)) {
            city = "南宁市";
        }
        LOGGER.error(currLoc);

        String type = request.getParameter("type");
        String year1 = request.getParameter("year1");
        String year2 = request.getParameter("year2");
        String year3 = request.getParameter("year3");
        List<String> years = new ArrayList<String>();
        if (!"".equals(year1)) {
            years.add(year1);
        }
        if (!"".equals(year2)) {
            years.add(year2);
        }
        if (!"".equals(year3)) {
            years.add(year3);
        }
        // HttpSession session = request.getSession();// 得到session
        String zfValue = request.getParameter("zf");
        String dayTime = request.getParameter("daytime");
        String ym = request.getParameter("ym");
        System.out.println("type=" + type);
        System.out.println(years);

        if ("01".equals(zfValue)) {// 查询的是某一天的客流量
            StringBuffer sb = new StringBuffer();
            sb.append(city + "<span color='red'>" + dayTime + "</span>日客流量统计数据为<span color='red'>");
            sb.append(SPOT_DAO.getStatisticsNumberByCityAndDate(currLoc, 2, dayTime));
            sb.append("</span>");
            out.write(sb.toString().getBytes("utf-8"));
        } else if ("02".equals(zfValue)) {// 查询的是本周的客流量
            StringBuffer sb = new StringBuffer();
            sb.append(city + "本周<span color='red'>" + "0000-00-00" + "</span>客流量统计数据为<span color='red'>");
            sb.append(SPOT_DAO.getStatisticsNumberByCityAndDate(currLoc, 2, null));
            sb.append("</span>");
            out.write(sb.toString().getBytes("utf-8"));
        } else if ("03".equals(zfValue)) {// 查询的是某一月的客流量
            StringBuffer sb = new StringBuffer();
            sb.append(city + "<span color='red'>" + ym.substring(0, 4) + "年" + ym.substring(5) + "月"
                    + "</span>客流量统计数据为<span color='red'>");
            sb.append(SPOT_DAO.getStatisticsNumberByCityAndDate(currLoc, 2, ym));
            sb.append("</span>");
            out.write(sb.toString().getBytes("utf-8"));
        } else {// 某些年的统计数据
            // 判断时间的长度
            JFreeChart chart = null;
            if (!years.isEmpty()) {
                if (type.equals("line")) {
                    chart = drawLine(currLoc, city, out, years);
                }
                if (type.equals("bar")) {
                    chart = drawBar(currLoc, city, out, years);
                }
                if (type.equals("pie")) {
                    chart = drawPie(currLoc, city, out, years);
                }
            }
            StandardEntityCollection sec = new StandardEntityCollection();
            ChartRenderingInfo info = new ChartRenderingInfo(sec);
            PrintWriter w = new PrintWriter(out);
            ChartUtils.writeImageMap(w, "img", info, false);
            response.setContentType("image/jpeg;charset=utf-8 ");
            ChartUtils.writeChartAsJPEG(out, chart, 400, 300);
            out.flush();
            out.close();
        }
    }

    private JFreeChart drawPie(String cityCode, String city, OutputStream out, List<String> years) {
        JFreeChart chart = new DrawPie(cityCode, city, years, 2).yearSpotPie();
        return chart;
    }

    private JFreeChart drawBar(String cityCode, String city, OutputStream out, List<String> years) {
        DrawBar spotbar = null;
        CategoryDataset categorydatasets = null;
        JFreeChart chart = null;
        spotbar = new DrawBar(cityCode, city, years, 2);
        categorydatasets = spotbar.getDataSet(years.size());
        chart = spotbar.feng(categorydatasets);
        return chart;
    }

    private JFreeChart drawLine(String cityCode, String city, OutputStream out, List<String> years) {
        DrawLine spotline = null;
        XYDataset xydatasets = null;
        JFreeChart chart = null;
        spotline = new DrawLine(cityCode, city, years, 2);
        xydatasets = spotline.createDatasets(years.size());
        chart = spotline.yearSpotLine(xydatasets);
        return chart;
    }
}
